package routers

import (
	"net/http"
	"punch/dbs"

	"github.com/gin-gonic/gin"
)

func LoadUser(e *gin.Engine) {
	e.POST("/punc/user/get", userGetHandler)
}

// 获取用户信息
func userGetHandler(c *gin.Context) {
	tempUserType, _ := c.Get("Usertype")
	tempUserId, _ := c.Get("UserID")

	db, err := dbs.Conn(c)
	if err != nil {
		db.Close()
		return
	}

	switch tempUserType {
	case "manager":
		var manager []Manager
		err := db.Select(&manager, "select * from manager where id=?", tempUserId)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 20004,
				"msg":  err.Error(),
			})
			return
		}
		if len(manager) == 0 {
			c.JSON(http.StatusOK, gin.H{
				"code": 20010,
				"msg":  "该用户不存在",
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 20000,
			"msg":  "获取成功",
			"data": gin.H{
				"Id":          manager[0].Id,
				"Account":     manager[0].Account,
				"Name":        manager[0].Name,
				"Create_time": manager[0].Create_time,
				"Update_time": manager[0].Update_time,
				"Role":        "manager",
			},
		})
	case "teacher":
		var teacher []Teacher
		err = db.Select(&teacher, "select t.id, t.org_id, org.name as org_name, t.name, t.phone, t.email, t.duty, t.tno, t.type, t.create_time, t.update_time"+
			" from teacher as t left join org on t.org_id=org.id where t.id=?", tempUserId)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 20004,
				"msg":  err.Error(),
			})
			return
		}
		if len(teacher) == 0 {
			c.JSON(http.StatusOK, gin.H{
				"code": 20010,
				"msg":  "该用户不存在",
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 20000,
			"msg":  "获取成功",
			"data": gin.H{
				"Id":          teacher[0].Id,
				"Org_id":      teacher[0].Org_id,
				"Org_name":    teacher[0].Org_name,
				"Name":        teacher[0].Name,
				"Phone":       teacher[0].Phone,
				"Email":       teacher[0].Email,
				"Duty":        teacher[0].Duty,
				"Tno":         teacher[0].Tno,
				"Type":        teacher[0].Type,
				"Create_time": teacher[0].Create_time,
				"Update_time": teacher[0].Update_time,
				"Role":        "teacher",
			},
		})
	case "student":
		var student []Student
		err := db.Select(&student, "select * from student where id=?", tempUserId)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 20004,
				"msg":  err.Error(),
			})
			return
		}
		if len(student) == 0 {
			c.JSON(http.StatusOK, gin.H{
				"code": 20010,
				"msg":  "该用户不存在",
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 20000,
			"msg":  "获取成功",
			"data": gin.H{
				"Id":          student[0].Id,
				"Org_id":      student[0].Org_id,
				"Name":        student[0].Name,
				"Sex":         student[0].Sex,
				"Phone":       student[0].Phone,
				"Email":       student[0].Email,
				"Class":       student[0].Class,
				"Apartment":   student[0].Apartment,
				"Avatar":      student[0].Avatar,
				"Note":        student[0].Note,
				"Credit":      student[0].Credit,
				"Points":      student[0].Points,
				"Peract_repu": student[0].Peract_repu,
				"Triact_repu": student[0].Triact_repu,
				"Sno":         student[0].Sno,
				"Type":        student[0].Type,
				"Create_time": student[0].Create_time,
				"Update_time": student[0].Update_time,
				"Role":        "student",
			},
		})
	default:
		c.JSON(http.StatusOK, gin.H{
			"code": 20010,
			"msg":  "该用户不存在",
		})
	}

	db.Close()
}
